<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


 
 
class Welcome extends CI_Controller{
	
	
	public function process()
	{
		$this->load->model('login');
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->helper('form');
		//Make sure login info was sent
        if($this->input->post('username') == '') {
           // return false;
		  redirect('/welcome', 'refresh');
        }
		
		 //Check if already logged in
        if($this->session->userdata('logged_in') == $this->input->post('username')) {
			//User is already logged in.
             redirect('/welcome', 'refresh');
        }
		
		if($this->login->login())
		{
			//if user found
			//check pasword - not now
			/*
				check password function
			*/
			
			//to prevent double logins
			$this->session->sess_destroy();
			
			//create new
			$this->session->sess_create();
			
			$this->session->set_userdata(array('logged_in' => true));
			
			 redirect('/welcome', 'refresh');
		}else {
			redirect('/welcome', 'refresh');
		}
		
	}
	
	public function logout() {
		$this->load->helper('url');
		$this->session->sess_destroy();
		redirect('/welcome', 'refresh');
	}
	
	
	public function index()
	{
		$this->load->library('session');
		$this->load->helper('form');
		$this->load->model('product');
		$this->load->model('message');
		$this->load->model('order');
		
		
		//on index page load welcome_message field that required login
		 if($this->session->userdata('logged_in') == true)
		 {
			//create warning
			$warnings ="";
			if($this->product->getWarnings())
			{
				$warnings = "<p>Following products are low on stock</p>
				<ul>
				";

				foreach($this->product->getWarnings() as $war)
				{
					$warnings .= "<li>{$war->name} - left {$war->quantity}</li>";
				}
			
				$warnings .="</ul>";
			}
			//creating products list 
			$productList = "<table>
				<th>ID</th><th>Name</th><th>Price</th><th>Qt</th>
			";
			foreach($this->product->getProductQuantity() as $pro)
			{
				$productList .= "<tr>
					<td>
						{$pro->id}
					</td>
					<td>
						{$pro->name}
					</td>
					<td>
						{$pro->price}
					</td>
					<td>
						{$pro->quantity}
					</td>
				</tr>";
			}
			$productList .= "</table>";
			
			//creating messages for manager
			$messages = "<table>
			<th>Date</th><th>Title</th><th>Message</th>";
			foreach($this->message->getMessage() as $mess)
			{
				$messages .= "<tr "; 
				if(!$mess->readed)
				{
					$messages .=" class=\"read\"";
				}
				$messages .=">";
				$messages .= "<td>{$mess->date}</td>";
				$messages .= "<td>{$mess->title}</td>";
				$messages .= "<td>{$mess->message}</td>";
				$messages .="</tr>";
			}
			$messages .= "</table>"; 
			
			//creating order
			
			$orders = "<table>
			<th>ID</th><th>Customer</th><th>Total</th><th>Date</th>";
			$intT = 1;
			foreach($this->order->getOrder() as $ord) {
				$class = "odd";
				if($intT%2 == 0)
				{
					$class = "even";
				}
				$orders .="<tr class='{$class}'>";
				$orders .="<td>{$intT}</td><td>{$ord['name']}</td><td>&pound;{$ord['total']}</td><td>{$ord['date']}</td>";
				$orders .="</tr>";
				$intT++;
			}
			$orders .= "</table>";
			
			$data['productA'] = $productList;
			$data['messageA'] = $messages;
			$data['orderA'] = $orders;
			$data['warningA'] = $warnings;
			
			
			$this->session->set_userdata(array('warning' => $warnings));
			
			
			$this->load->view('system', $data);
		 
		 }else {	
			
			//load logn view
			$this->load->view('login');
		}
	}
}

